import ajax from './../utils/ajax.js'
import utils from './../utils/index.js'
import config from './../config.js'

var did = utils.getParameterByName('did') || 2
var urls = config.urls

var navConfig;

var store = { navConfig }

var cache = {}

var limit = 10

var getNavItem = function (cid) {
    //var item = navConfig.channels.filter(val => val.i == cid);
    //var item = navConfig.channels.filter(function (val) {
    //    return val.i == cid
    //})[0];
    var item;

    navConfig.channels.forEach(function (val) {
        if(val.i == cid){
            item = val;
        }
    })
    return item
}

var removeCache = function (data) {
    if(Object.keys(data.ids).length >= 20){
        data.ids.splice(0, 9);
        data.cache.splice(0, 9);
    }
}

var getListParams = function (cid, tid, key) {
    var lt;
    var page = cache[key].page;
    var start = page * limit;
    var end = (page + 1) * limit - 1;

    var data = {did, cid, start, end};

    if(typeof tid === 'undefined'){
        let item = getNavItem(cid);
        lt = item.t;
    }else{
        lt = 9;
        data.tid = tid;
    }

    data.lt = lt;

    return data
}

store.getNavItem = getNavItem;

store.lastScrollTop = 0;

store.setScrollTop = function (cid) {
    var item = getNavItem(cid);
    item.scrollTop = store.lastScrollTop;
}

store.getScrollTop = function (cid) {
    var item = item = getNavItem(cid);
    return item.scrollTop || 0
}

store.getNav = function () {
    "use strict";
    return new Promise((resolve, reject) => {
        let url = urls.nav;
        let config = { data: { did } };
        ajax.xhr(url, config, function (data) {
            if(data.status){
                let _data = data.data;
                store.navConfig = _data;
                navConfig = store.navConfig;
                resolve(_data.channels);
            }else{
                reject();
            }
        }, reject)
    })
}

store.getDetail = (id, cid, tid) => {
    "use strict";
    return new Promise((resolve, reject) => {
        let obj = {did, cid, jid: id};

        if(Number(tid) !== -1){
            obj.tid = tid;
            obj.lt = 9;
        }else{
            obj.lt = getNavItem(cid).t
        }

        let config = {data: obj}

        ajax.xhr(urls.detail, config, function (data) {
            resolve(data.data)
        }, reject);
    })
}

var insertAds = function (arr) {
    var adConfig = navConfig.adConfig;

    if(!adConfig){
        return arr
    }

    var interval = adConfig.s;
    var slot = adConfig.lc;
    var index = 0;
    var adsBlock = [];
    var ads = {slot, type: 'ads'};

    if(interval && interval > 0){
        arr.forEach(function (val, i) {
            if(val.type === 'ads'){
                index = 0;
            }else{
                index++
            }

            if(index > interval){
                adsBlock.push(i);
                index = 0;
            }
        })

        adsBlock.forEach(function (val, i) {
            arr.splice(val, 0, ads)
        })
    }

    return arr;

}

store.getTidTitle = function (cid, tid) {
    var item = '';
    try {
        cache[cid].list.forEach(function (val) {
            if(val.id == tid){
                item = val.title
            }
        })
        //item = cache[cid].list.filter(function (val) {
        //    return val.id = tid
        //})[0].title;
    }catch(e){}

    return item;
}

store.getList = (cid, tid, isRoute) => {
    "use strict";
    let key = typeof tid !== 'undefined' ? 's' + cid + '-' + tid : cid;

    if(!cache.hasOwnProperty(key)){
        cache[key] = {
            page: 0,
            list: []
        }
    }

    store.history = {cid}

    if(tid){
        store.history.tid = tid;
    }


    return new Promise((resolve, reject) => {
        let config = {data: getListParams(cid, tid, key)};

        if(isRoute && cache[key].list.length){
            resolve(cache[key].list);
            return;
        }

        ajax.xhr(urls.list, config, function (data) {
            if(data && data.data){
                var _data = data.data;
                var cidStore = cache[key];
                var newList;
                cache[key].page = cidStore.page + 1;
                newList = cidStore.list.concat(_data);
                cache[key].list = insertAds(newList);
                resolve(cache[key].list);
            }else{
                reject();
            }
        }, reject);
    });
}

store.vote = function (id, type) {
    ajax.xhr(urls.vote, {data: {id, type}}, function (data) {
        console.log(data)
    })
}

store.getAds = (slot) => {
    return new Promise((resolve, reject) => {
        let config = { data: { slot }}
        ajax.xhr(urls.ads, config, function (data) {
            resolve(data)
        }, reject);
    })
}

store.feedback = ( q, success, error) => {
    "use strict";
    q.distributorId = did;

    ajax.xhr(urls.feedback, {data: q}, function (data) {
        if(data && data.status == 1){
            success(data)
        }else{
            error()
        }
    }, error)
}


export default store
























